package com.jingdianjichi.subject.domain.handler.subject;

import com.jingdianjichi.subject.common.enums.IsDeleteFlagEnum;
import com.jingdianjichi.subject.common.enums.SubjectInfoTypeEnum;
import com.jingdianjichi.subject.domain.convert.JudgeSubjectConverter;
import com.jingdianjichi.subject.domain.entity.SubjectAnswerBO;
import com.jingdianjichi.subject.domain.entity.SubjectInfoBO;
import com.jingdianjichi.subject.domain.entity.SubjectOptionBO;
import com.jingdianjichi.subject.domain.handler.SubjectTypeHandler;
import com.jingdianjichi.subject.infra.basic.entity.SubjectJudge;
import com.jingdianjichi.subject.infra.basic.service.SubjectJudgeService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
public class JudgeTypeHandler implements SubjectTypeHandler{

    @Resource
    private SubjectJudgeService subjectJudgeService;

    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.JUDGE;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        ArrayList<SubjectJudge> judgeList = new ArrayList<>();
        List<SubjectAnswerBO> answerBoList = subjectInfoBO.getOptionList();
        if (!answerBoList.isEmpty()) {
            answerBoList.forEach(answer-> {
                SubjectJudge judge = new SubjectJudge();
                SubjectJudge convertAnswer = JudgeSubjectConverter.INSTANCE.converBoToEntity(answer);
                judge.setIsCorrect(convertAnswer.getIsCorrect());
                judge.setSubjectId(subjectInfoBO.getId());
                judge.setIsDelete(IsDeleteFlagEnum.UN_DELETED.getCode());
                judgeList.add(judge);
            });
        }
        subjectJudgeService.batchInster(judgeList);

//        SubjectJudge subjectJudge = new SubjectJudge();
//        SubjectAnswerBO subjectAnswerBO = subjectInfoBO.getOptionList().get(0);
//        subjectJudge.setSubjectId(subjectInfoBO.getId());
//        subjectJudge.setIsCorrect(subjectAnswerBO.getIsCorrect());
//        subjectJudge.setIsCorrect(IsDeleteFlagEnum.UN_DELETED.getCode());
//        subjectJudgeService.insert(subjectJudge);
    }

    @Override
    public SubjectOptionBO query(Integer subjectId) {
        SubjectJudge judge = new SubjectJudge();
        judge.setSubjectId(subjectId);
        List<SubjectJudge> subjectJudgeByQueryList =  subjectJudgeService.queryBySubjectId(judge);
        List<SubjectAnswerBO> answerBoList = JudgeSubjectConverter.INSTANCE.converterJudgeListToBo(subjectJudgeByQueryList);
        SubjectOptionBO optionBo = new SubjectOptionBO();
        optionBo.setOptionList(answerBoList);
        return optionBo;
    }
}
